package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.dao.ProductProcessMaterialDao;
import com.wing.product.model.entity.ProductProcessMaterial;
import com.wing.product.model.request.ProductProcessMaterialForm;
import com.wing.product.model.response.ProductProcessMaterialVO;
import com.wing.system.model.response.FactoryInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 产品工序物料表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductProcessMaterialServiceImpl extends ServiceImpl<ProductProcessMaterialDao, ProductProcessMaterial> implements ProductProcessMaterialService {

    @Override
    public int add(ProductProcessMaterialForm productProcessMaterialForm) {

        //校验

        ProductProcessMaterial productProcessMaterial =new ProductProcessMaterial();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductProcessMaterial.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productProcessMaterialForm, productProcessMaterial, copyOptions);

        return baseMapper.insert(productProcessMaterial);
    }

    @Override
    @Transactional
    public int add(List<ProductProcessMaterialForm> productProcessMaterialForm) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sku_code",productProcessMaterialForm.get(0).getSkuCode());
        this.remove(queryWrapper);
        for (ProductProcessMaterialForm processMaterialForm : productProcessMaterialForm) {
            if(StringUtils.isBlank(processMaterialForm.getDefaultMaterialSkuCode())||StringUtils.isBlank(processMaterialForm.getAlternativeMaterialSkuCode())){
                return 1;
            }
        }
        //利用json 实现list 拆包解包
        List<ProductProcessMaterial> factoryInfoVOS = JSON.parseObject(JSON.toJSONString(productProcessMaterialForm), new TypeReference<List<ProductProcessMaterial>>() {
        });
        boolean b = this.saveBatch(factoryInfoVOS);
        if(b){
            return 1;
        }else {
            return  0;
        }

    }

    @Override
    public List<ProductProcessMaterialVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductProcessMaterialVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductProcessMaterialVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductProcessMaterialVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
